package net.starwifi.duobao.web.db;

import net.starwifi.duobao.entity.DBProduct;
import net.starwifi.duobao.entity.DBTimes;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.service.DBProductService;
import net.starwifi.duobao.service.DBTimesService;
import net.starwifi.duobao.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by Administrator on 2015/11/19.
 */
@Controller("apiDBCartController")
@RequestMapping("/db/cart")
public class CartController extends BaseController {


    @Autowired
    DBProductService dbProductService; //夺宝产品

    @Autowired
    DBTimesService dbTimesService; //历史期

    @Autowired
    MemberService memberService;

    /**
     * 产品购买
     * @param tNo 期数
     * @param pid 商品ID
     * @param num 购买数量
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String userRecord(Model model,String tNo,Long pid,Integer num) {
        DBProduct dbProduct=dbProductService.find(pid);//夺宝产品
        if(dbProduct==null){ //产品存在
            model.addAttribute("content","产品不存在");
            return "/db/error";
        }
        DBTimes dbTimes=dbTimesService.findByTimesNo(tNo);
        if(dbTimes==null){ //不存在的夺宝期
            model.addAttribute("content","夺宝期不存在");
            return "/db/error";
        }
        if(dbTimes.getIsOver()){ //已经结束的不能购买
            model.addAttribute("content","夺宝期已经结束");
            return "/db/error";
        }
        model.addAttribute("dbProduct",dbProduct);
        model.addAttribute("dbTimes",dbTimes);
        model.addAttribute("num",num);
        return "/db/cart-list";
    }


}
